/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameheart.client.logic;

import gameheart.client.sprite.ImageResposity;
import gameheart.client.sprite.LabelSprite;
import gameheart.client.sprite.ImageSprite;
import gameheart.client.sprite.Sprite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.io.File;

/**
 *
 * @author win7
 */
public class MenuState extends  State {


    MenuState(MainGame mainGame) {
       super(mainGame);
    }

    public void gameUpdate() {
        if (_menuSelected > -1) {
            MenuClick(_menuSelected);
        }
    }

    public void gameRender(Graphics g) {
        g.drawImage(background, 0, 0, 1024, 738 - 30, _mainGame);
        g.setColor(Color.yellow);
        // avatar
//        for (int index = 0; index < _sprites.size(); index++) {
//            _sprites.get(index).draw(g);
//        }
        // menu
        for (int i = 0; i < _menus.size(); i++) {
            _menus.get(i).draw(g);
        }
    }
    Image background;
   // ArrayList<Sprite> _sprites = new ArrayList<Sprite>();
    ArrayList<LabelSprite> _menus = new ArrayList<LabelSprite>();
    ImageResposity imageResposity;

    public void gameInit() {
        imageResposity = new ImageResposity();
        background = imageResposity.getImage("bg_main.jpg");

      //  _sprites.add(new ImageSprite(805, 137, 60, 60, imageResposity, "avata_default.gif"));

        BufferedImage menuBackGround = imageResposity.getImage("menu.jpg");
        _menus.add(new LabelSprite(800, 425,300, 30, "Create game", menuBackGround));
        _menus.add(new LabelSprite(800, 465,300, 30 ,"Join game", menuBackGround));
       // _menus.add(new LabelSprite(800, 505,300, 30, "Help", menuBackGround));
        _menus.add(new LabelSprite(800, 545,300, 30,  "Exit", menuBackGround));
    }
    volatile private int _menuSelected = -1;

    private void ProcessMouseClick(MouseEvent e) {

        Rectangle mouseRect = new Rectangle(e.getX() - _mainGame.getInsets().left, e.getY() - _mainGame.getInsets().top, 1, 1);
        int menuSelectedIndex = -1;
        for (Sprite currentSpirite : _menus) {

            if (currentSpirite.isIntersec(mouseRect)) {
                menuSelectedIndex = _menus.indexOf(currentSpirite);
                break;
            }
        }

        _menuSelected = menuSelectedIndex;
    }

    private void MenuClick(int menuIndex) {
        switch (menuIndex) {
            case 0: {
                _mainGame.ChangeGameState(GameStateIds.CreateGameState);
                break;
            }
            case 1: {
                _mainGame.ChangeGameState(GameStateIds.JoinStateId);
                break;
            }
            case 3: {
                break;
            }
        }
        _menuSelected = -1;
    }

    public void gameEnd() {
    }

    private void OpenHelp() {
        try {
            File file = new File(".");
            String path = file.getAbsolutePath();
            path = path.substring(0, path.length() - 1);
            path = path + "Help.chm";
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + path);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void mouseClicked(MouseEvent e) {
        ProcessMouseClick(e);
    }

    public void keyPressed(KeyEvent e) {
        
    }
}
